#include "leftsiderbarwidget.h"
#include <QDebug>
#include <QStyleOption>
#include <QPainter>
#include "../common/mydefine.h"
#include "component/myiconbutton.h"
#include "globalbackupinfo.h"
#include "gsettingswrapper.h"

#define PERSONALSIE_SCHEMA     "org.ukui.control-center.personalise"
#define PERSONALSIE_TRAN_KEY   "transparency"
#define CONTAIN_PERSONALSIE_TRAN_KEY   "transparency"

LeftsiderbarWidget::LeftsiderbarWidget(QWidget *parent, StartMode mode)
    : QWidget(parent),
      m_navigationBar(nullptr)
{
    m_parent = parent;
    m_mode = mode;

    m_personalQgsettings = nullptr;
    if (QGSettings::isSchemaInstalled(PERSONALSIE_SCHEMA)) {
        m_personalQgsettings = new QGSettings(PERSONALSIE_SCHEMA, QByteArray(), this);
        m_transparency = m_personalQgsettings->get(PERSONALSIE_TRAN_KEY).toDouble();
        connect(m_personalQgsettings,&QGSettings::changed,this,[=](QString changedKey) {  //监听透明度变化
            if (changedKey == CONTAIN_PERSONALSIE_TRAN_KEY) {
                m_transparency = m_personalQgsettings->get(PERSONALSIE_TRAN_KEY).toDouble();
                // this->setWindowOpacity(m_transparency);
                this->repaint();
            }
        });
    }
}

void LeftsiderbarWidget::initUi()
{
    m_leftSideBarVLayout = new QVBoxLayout;
    m_leftSideBarVLayout->setObjectName("m_leftSideBarVLayout");
    m_leftSideBarVLayout->setContentsMargins(0,0,0,0);
    m_leftSideBarVLayout->setSpacing(5);
    setLayout(m_leftSideBarVLayout);

    QWidget *titleWidget = new QWidget(this);
    m_leftSideBarVLayout->addWidget(titleWidget);

    QHBoxLayout * titleLayout = new QHBoxLayout;
    m_mTitleIcon  = new PixmapLabel;
    m_mTitleIcon->setFixedSize(24, 24);
    m_mTitleIcon->setThemeIconSchema(THEME_YHKYLIN_BACKUP_TOOLS, ":/images/yhkylin-backup-tools.png");
    m_titleLabel  = new MyLabel;
    m_titleLabel->setFixedWidth(this->width() - 40);
    m_titleLabel->setElidedText(tr("Backup & Restore"));
    if (m_titleLabel->text() != tr("Backup & Restore"))
        m_titleLabel->setToolTip(tr("Backup & Restore"));
    m_titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    titleWidget->setLayout(titleLayout);
    titleLayout->addWidget(m_mTitleIcon);
    titleLayout->addWidget(m_titleLabel);
    m_leftSideBarVLayout->addSpacing(10);

    // 功能列表
    int funcNum = FuncTypeConverter::FunType::TOTALMODULES;
    if (StartMode::livecd == m_mode) {
        m_deplayFuncs << "BACKUP_SYSTEM" << "RESTORE_SYSTEM";
    } else if (StartMode::std_user == m_mode) {
        m_deplayFuncs << "BACKUP_DATA" << "RESTORE_DATA" << "OPERATION_LOG";
    } else {
        m_deplayFuncs << "BACKUP_SYSTEM" << "RESTORE_SYSTEM" << "BACKUP_DATA" << "RESTORE_DATA" << "OPERATION_LOG" << "GHOST_IMAGE";
    }

    m_idxToFunctype.clear();
    int index = 0;
    m_navigationBar = new KNavigationBar(this);
    m_navigationBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    FuncTypeConverter kvConverter;
    for (int type = 0; type < funcNum; ++type) {
        QString mnameString  = kvConverter.keycodeTokeystring(type); //设置TEXT
        if (!m_deplayFuncs.contains(mnameString))
            continue ;

        QStandardItem * item = new QStandardItem;

        QString themeIconName = kvConverter.keycodeToThemeIconString(type);
        QString mnamei18nString  = kvConverter.keycodeTokeyi18nstring(type); //设置TEXT
        QString defaultIconName(":/symbos/");
        defaultIconName += themeIconName;
        QIcon icon = QIcon::fromTheme(themeIconName, QIcon(defaultIconName));
        item->setIcon(icon);
        item->setText(mnamei18nString);
        item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        // item->setToolTip(mnamei18nString);
        item->setCheckable(true);
        m_navigationBar->addItem(item);

        m_idxToFunctype.insert(index, type);
        m_functypeToIdx.insert(type, index);
        ++index;
    }
    connect(m_navigationBar->listview(), &QListView::clicked, [=](const QModelIndex &index) {
        int row = index.row();
        int type = this->m_idxToFunctype.value(row);
        emit this->selected(type);
    });

    connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::busy, this, [=](bool isBusy) {
        bool enabled = !isBusy;
        this->m_navigationBar->setEnabled(enabled);
    });

    QHBoxLayout * hbarLayout = new QHBoxLayout;
    hbarLayout->setContentsMargins(10, 0, 10, 0);
    hbarLayout->addWidget(m_navigationBar);
    m_leftSideBarVLayout->addLayout(hbarLayout);

    m_leftSideBarVLayout->addSpacing(8);
    m_leftSideBarVLayout->addStretch();
}

LeftsiderbarWidget::~LeftsiderbarWidget()
{

}

void LeftsiderbarWidget::setCheckedFunc(int func_type)
{
    FuncTypeConverter kvConverter;
    QString mnameString  = kvConverter.keycodeTokeystring(func_type); //设置TEXT
    if (!m_deplayFuncs.contains(mnameString))
        return ;

    m_navigationBar->listview()->setCurrentIndex(m_navigationBar->model()->index(m_functypeToIdx.value(func_type), 0));
}

void LeftsiderbarWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    p.setPen(Qt::NoPen);
    QColor color = palette().color(QPalette::Window);
    if (m_parent != nullptr) {
        color = m_parent->palette().color(QPalette::Window);
    }
    color.setAlphaF(m_transparency);
    QPalette pal(this->palette());
    pal.setColor(QPalette::Window, QColor(color));
    this->setPalette(pal);
    QBrush brush = QBrush(color);
    p.setBrush(brush);
    p.drawRoundedRect(opt.rect,0,0);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

    m_titleLabel->setElidedText(tr("Backup & Restore"));
    if (m_titleLabel->text() != tr("Backup & Restore"))
        m_titleLabel->setToolTip(tr("Backup & Restore"));
}
